package com.crm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.pojo.SalesOpportunity;
import com.crm.model.pojo.Solution;
import com.crm.services.CustomerServices;
import com.crm.services.DictionaryServices;
import com.crm.services.SolutionService;
import com.crm.services.UserServices;

@Controller
@RequestMapping("/sales")
public class SolutionController {
	
	@Autowired
	private SolutionService ss;
	@Autowired
	private DictionaryServices ds;
	@Autowired
	private CustomerServices cs;
	@Autowired
	private UserServices us;
	
	/**solution
	 * 解决方案初始页
	 * @return
	 */
	@RequestMapping("/solution")
	public ModelAndView findAllDetailedDemand() {
	
		String[] DId=new String[] {"全部数据"};
		JSONArray array=ss.ListTransJSONArray(0,4,DId);//分页查询所有	
		ModelAndView mv=new ModelAndView("sales/solution");
		mv.addObject("salesCount",ss.queryByCount());
		mv.addObject("salesOpp",array);
		System.out.println("解决方案");
		return mv;
	}
	/**
	 * 解决方案的分页查询
	 * @param <E>
	 * @param pageNumberl
	 * @param pageCentent
	 * @return
	 */
	@RequestMapping("/solutionPageable")
	@ResponseBody
	public  JSONObject salePageable(Integer pageNumber,Integer pageCentent, String[] DId) {
		System.out.println("pageNumber:"+pageNumber);
		System.out.println("pageCentent:"+pageCentent);
		System.out.println("DId的长度:"+DId.length);		
		int a=0;
		JSONObject json=new JSONObject();
		for (String string : DId) {
			System.out.println("DId"+a+":"+string);		 
			a+=1;
		}
		json.put("saleOpp",ss.ListTransJSONArray(pageNumber-1,pageCentent,DId));
		json.put("salesCount", ss.queryByCount());
		return json;			
	}
	
	/***
	 * 解决方案新增时的数据字典信息
	 * @return
	 */
	@RequestMapping("/saleAddsolutionInfo")
	@ResponseBody
	public JSONArray saleAddDetailedDemandInfo() {	
		System.out.println("saleAddDetailedDemandInfo");
		JSONArray array=new JSONArray();
		 array.add(ds.ListTransJSONArray(ds.findByDictParentid(1074)));//竞争能力(1074)
		return array;			
	}
	
	/**
	 *内表，通过多条件+模糊查询对应的负责人名称
	 * @return
	 */
/*	@RequestMapping("/withinsaleAddUnameInfo")
	@ResponseBody
	public JSONArray withinsaleAddUnameInfo(String[] DId) {	
		System.out.println("withinsaleAddUnameInfo");
			return ss.ListTransJSONArray2(ss.queryByAll(0, 5, DId));
	
	}	
	*/
	
	/**
	 *内表，通过多条件+模糊查询对应的机会主题名称
	 * @return
	 */
	@RequestMapping("/withinsolutionFindByTheme")
	@ResponseBody
	public JSONArray withinSaleFindByTheme(String[] DId) {	
		System.out.println("withinsolutionFindByTheme");
			return ss.ListTransJSONArray3(ss.queryByAll(0, 5, DId));
	
	}	

	/**
	 * 外表,通过模糊查询对应的客户名称
	 */
	@RequestMapping("/abroadsolutionCustName")
	@ResponseBody
	public JSONArray saleCustName(String custName) {
		System.out.println("外表，触发销售机会对应客户字段的模糊查询:"+custName);
		if(custName==null) {
			System.out.println(cs.findAll());
			return cs.ListTransJSONArray(cs.findAll());
		}else {
			return cs.ListTransJSONArray(cs.findByCustNameLike(custName));
		}		
	}
	
	/**
	 * 外表,通过模糊查询对应的客户名称再查询出该客户的所有销售机会
	 */
	@RequestMapping("/abroadsolutionsales")
	@ResponseBody
	public JSONArray findBysales(Integer custID) {
		System.out.println("外表，触发销售机会对应客户字段的模糊查询:"+custID);
		if(custID==null) {
			return null;
		}else {
			return cs.ListTransJSONArray1(cs.findByCustID(custID));
		}		
	}
	
	
	/**
	 * 内表，通过多条件+模糊查询对应的解决方案主题
	 * @param DId
	 * @return
	 */
	@RequestMapping("/withinsolutioncomName")
	@ResponseBody
	public JSONArray withinDetailedDemandTheme(String[] DId) {
		System.out.println("内表，通过多条件+模糊查询对应的公司名名字");
		return ss.ListTransJSONArray4(ss.queryByAll(0, 5, DId));
		
	}
	/**
	 * 内表,通过多条件+模糊查询对应的客户名称
	 */
	@RequestMapping("/withinsolutionCustName")
	@ResponseBody
	public JSONArray saleCustName(String[] DId) {
		System.out.println("内表，触发解决方案对应客户字段的模糊查询:"+DId);
		System.out.println("DId的长度:"+DId.length);	
			return ss.ListTransJSONArray1(ss.queryByAll(0, 5, DId));		   		
	}	
	/**
	 *查找单个解决方案
	 * @return
	 */
	@RequestMapping("/findOnesolution")
	@ResponseBody
	public JSONArray findOnesolution(Integer salId) {	
		System.out.println("findOneSale"+salId);
		JSONArray array =new JSONArray();
		array.add(ss.findBySoId(salId));//查询单个解决方案
		
		return array;			
	}
	/**
	 * 确认解决方案是否重复
	 */
	@RequestMapping("/findBySolTheme")
	@ResponseBody
	public Integer findBySalTheme(String salTheme) {
		System.out.println("触发确认解决方案是否重复:"+salTheme);			

			return ss.findBySolTheme(salTheme);		
	}
	
	/**
	 *新增 解决方案
	 * @return
	 */
	@RequestMapping("/Addsolution")
	@ResponseBody
	public int Addsale(@ModelAttribute("sale") Solution sale) {	
		System.out.println("dddddddddddddddddddddddddddddddddddddd");
		System.out.println("Addsolution:"+sale.getSolTheme());
		return ss.save(sale);			
	}
	
	/**
	 *删除单个解决方案
	 * @return
	 */
	@RequestMapping("/solutionDelete")
	@ResponseBody
	public int Delete(Integer salId) {	
		System.out.println("Delete:"+salId);
		ss.Delete(salId);
		return 1;			
	}
}
